package com.hunt.service.impl;


import com.hunt.dao.UserLevelMapper;
import com.hunt.model.dto.PageInfo;

import com.hunt.model.dto.UserLevel;
import com.hunt.service.UserLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shanchao
 * @create 2019/11/5-9:42
 */
@Service
@Transactional
public class UserLevelServiceImpl implements UserLevelService {

    @Autowired
    private UserLevelMapper userLevelMapper;

    /**
     * 保存UserLevel数据到表
     * @param userLevel
     * @return
     */
    @Override
    public int insertUserLevel(UserLevel userLevel) {
        try {
            int insert = userLevelMapper.insert(userLevel);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 更新UserLevel数据到表
     * @param userLevel
     * @return
     */
    @Override
    public int update(UserLevel userLevel) {
        try {
            if (userLevel.getUserlevel().equals("体验账户")){
                userLevel.setUserlevel("0");
            }
            if (userLevel.getUserlevel().equals("租用账户")){
                userLevel.setUserlevel("1");
            }
            if (userLevel.getUserlevel().equals("经典账户")){
                userLevel.setUserlevel("2");
            }
            if (userLevel.getUserlevel().equals("尊享账户")){
                userLevel.setUserlevel("3");
            }
            if (userLevel.getUserlevel().equals("标准账户")){
                userLevel.setUserlevel("4");
            }
            if (userLevel.getUserlevel().equals("待定账户1")){
                userLevel.setUserlevel("5");
            }
            if (userLevel.getUserlevel().equals("待定账户2")){
                userLevel.setUserlevel("6");
            }
            int update = userLevelMapper.update(userLevel);
            return update;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据UserlevelId删除表中对应的UserLevel数据
     * @param userlevel
     * @return
     */
    @Override
    public int deleteByUserlevelId(String userlevel) {
        try {
            int i = userLevelMapper.deleteByUserlevel(userlevel);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据UserLevel对象查询UserLevel表中对应的数据
     * @param userLevel
     * @return
     */
    @Override
    public UserLevel selectUserLevel(UserLevel userLevel) {
        try {
            userLevelMapper.select(userLevel);
            return userLevel;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据UserLevel标识查询UserLevel表中对应的数据
     * @param userlevel
     * @return
     */
    @Override
    public UserLevel selectByUserLevel(String userlevel) {
        /*StringBuffer sf = new StringBuffer();
        StringBuffer append = sf.append("'").append(userlevel).append("'");
        System.out.println("append======"+append);*/
        UserLevel userLevel = null;

            userLevel = userLevelMapper.selectByUserLevel(userlevel);
            System.out.println("userlevel==="+userlevel);

        return userLevel;
    }

    /**
     * 查询UserLevel表中的所有数据未分页
     * @return
     */
    @Override
    public List<UserLevel> selectAll() {
        try {
            return userLevelMapper.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询UserLevel表中的所有数据记录数
     * @return
     */
    @Override
    public int selectCounts() {
        try {
            return  userLevelMapper.selectCounts();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 分页查询UserLevel表中的所有数据记录
     * @return
     */
    @Override
    public PageInfo selectPage(int start, int size, String order) {
        Map<String,Object> map = new HashMap<>();
        int pageNo = (start - 1) * size;
        map.put("pageNo",pageNo);
        map.put("pageSize",size);
        map.put("order",order);
        try {
            List<UserLevel> userLevels = userLevelMapper.selectByPage(map);
            int counts = userLevelMapper.selectCounts();
            PageInfo pageInfo = new PageInfo(counts,userLevels);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}





















